home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group02b.txt
/
000069_icon-group-sender_Tue Oct 8 13:13:31 2002.msg
< prev
next >
Wrap
Internet Message Format
|
2003-01-02
|
3KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.11.1/8.11.1) id g98KCPY27292
for icon-group-addresses; Tue, 8 Oct 2002 13:12:25 -0700 (MST)
Message-Id: <200210082012.g98KCPY27292@baskerville.CS.Arizona.EDU>
From: "Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net>
X-Newsgroups: comp.lang.icon
Subject: A Better Reverse / Map Example
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
Date: Tue, 08 Oct 2002 18:20:22 GMT
X-Complaints-To: abuse@verizon.net
To: icon-group@cs.arizona.edu
Errors-To: icon-group-errors@cs.arizona.edu
Status: RO
The book "The Icon Programming Language" has an important chapter called
"Mappings and Labelings". The major thrust of the chapter is that the
predefined map function can quickly perform a variety of transpositions. To
illustrate, an example of how map could be used to implement the reverse
function as a procedure.
One of the limitations of the map technique is that the number of characters
that can be transported is limited by the number of 8-bit characters, namely
256. The procedure version of reverse must therefore break up strings longer
than 256 into pieces of at most 256 characters, reverse them, and put them
together.
In the example in the book, this is done by recursion. This use of recursion
strikes me as somewhat awkward. As an alternative approach, I would
decompose long strings into 256 character pieces using reverse string
scanning, i.e. we scan the string, and start the scanning by moving to the
right end of the string. We then use move with a negative argument to chop
the string into pieces small enough to map. The technique is illustrated
here:
############################################################################
####
procedure reverse(s)
local result
static trans, labels, max
initial {
# Set up labels, trans, max for short reverse mappings
labels := "ab"
trans := "ba"
max := *labels
# use this to bootstrap to 256 character mappings
trans := reverse( result := string(&cset) )
labels := result
max := *labels
}
s ? {
tab( 0 )
result := ""
while result ||:= map( trans, labels, move( -max ) )
if ( s := tab( 1 ) ) >> "" then
result ||:= map( right( trans, *s ), left ( labels, *s ), s )
}
return result
end